package com.haoshuang.zhouzhoubang.ui.page.mine.invite.skill

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityChoosePassSkillListBinding
import com.haoshuang.zhouzhoubang.model.bean.SkillAddBean
import com.haoshuang.zhouzhoubang.ui.page.home.settings.skill.add.AddSkillLeftAdapter
import com.haoshuang.zhouzhoubang.ui.page.home.settings.skill.add.AddSkillRightAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.getString
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.removeAllAnimation
import com.mufeng.libs.utils.view.vertical

/**
 *  
 */
class ChoosePassSkillListActivity : BaseVMActivity<ChoosePassSkillListViewModel, ActivityChoosePassSkillListBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<ChoosePassSkillListActivity>()
        }
    }

    private val leftData = arrayListOf<SkillAddBean>()
    private val rightData = arrayListOf<SkillAddBean.Small>()

    private val leftAdapter = AddSkillLeftAdapter(leftData)
    private val rightAdapter = AddSkillRightAdapter(rightData)

    private var ids1 = ""
    private var ids2 = ""
    private var ids3 = ""

    override fun initIntent() {
        ids1 = intent.getString("id1s")
        ids2 = intent.getString("id2s")
        ids3 = intent.getString("id3s")
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.apply{
            titleBar.title = "服务类别"
            titleBar.leftImageView().click { finish() }

            binding.rvList.vertical().adapter = leftAdapter
            binding.rvList.removeAllAnimation()
            binding.rvList2.vertical().adapter = rightAdapter
            binding.rvList2.removeAllAnimation()

            leftAdapter.setOnItemClick { view, position, item ->
                leftData.forEach {
                    it.is_checked = item?.id == it.id
                }
                leftAdapter.notifyDataSetChanged()
                rightData.clear()
                rightData.addAll(item?.small ?: listOf())
                rightAdapter.notifyDataSetChanged()
            }

            binding.tvQrz.clickWithTrigger {
                val firstList = arrayListOf<SkillAddBean>()
                val secondList = arrayListOf<SkillAddBean.Small>()
                val thirdList = arrayListOf<SkillAddBean.Small.LittleCate>()
                val list = leftData.filter {
                    val list = it.small?.filter {
                        val list = it.little_cate?.filter { it.is_checked == true }
                        thirdList.addAll(list?: listOf())
                        list?.isNotEmpty() ?: false
                    }
                    secondList.addAll(list?: listOf())
                    list?.isNotEmpty() ?: false
                }
                firstList.addAll(list)
                if (thirdList.isEmpty()){
                    toast("请选择技能")
                    return@clickWithTrigger
                }
                val id1s = firstList.joinToString(","){it.id.toString()}
                val id2s = secondList.joinToString(","){it.id.toString()}
                val id3s = thirdList.joinToString(","){it.id.toString()}
                val skills = thirdList.joinToString(","){it.cate_name.toString()}

                setResult(200, Intent().apply {
                    putExtra("id1s", id1s)
                    putExtra("id2s", id2s)
                    putExtra("id3s", id3s)
                    putExtra("skills", skills)

                    putExtra("chooseNum", thirdList.size)
                })
                finish()
            }
        }
    }

    override fun initData() {
        vm.getAddSkillList(2)
    }

    override fun startObserve() {
        vm.getAddSkillListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                leftData.clear()
                leftAdapter.notifyDataSetChanged()
                rightData.clear()
                rightAdapter.notifyDataSetChanged()
            }
            onSuccess {
                dismissLoading()
                if (it?.data?.isNotEmpty() == true){
                    val list = it.data!!
                    val ids3List = ids3.split(",")
                    if (ids3.isNotEmpty() && ids3List.isNotEmpty()){
                        list.forEach {first ->
                            first.small?.forEach {second ->
                                second.little_cate?.forEach {third ->
                                    if (ids3List.contains(third.id.toString())){
                                       third.is_checked = true
                                    }
                                }
                                val thirds = second.little_cate?.filter { it.is_checked == null || it.is_checked == false }
                                second.is_checked = thirds != null && thirds.isEmpty()
                            }
                        }
                    }

                    leftData.clear()
                    leftData.addAll(list.filter { it.small?.isNotEmpty() == true }.apply {
                        this[0].is_checked = true
                    })
                    leftAdapter.notifyDataSetChanged()
                    rightData.clear()
                    rightData.addAll(leftData[0].small ?: listOf())
                    rightAdapter.notifyDataSetChanged()
                }else{
                    leftData.clear()
                    leftAdapter.notifyDataSetChanged()
                    rightData.clear()
                    rightAdapter.notifyDataSetChanged()
                }

            }
        }
    }

    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(true)
            statusBarColor(R.color.c_3b76c2)
            titleBarMarginTop(binding.root)
        }
    }

}